package com.qianyucc.blog.global;

import lombok.extern.slf4j.*;
import org.springframework.stereotype.*;
import org.springframework.web.servlet.*;

import javax.servlet.http.*;
import java.util.*;

/**
 * @author lijing
 * @e-mail 1413979079@qq.com
 * @date 2020-01-11 22:45
 * @description 拦截器：用于日志打印
 */
@Component
@Slf4j
public class LogInterceptor implements HandlerInterceptor {
    @Override
    public synchronized boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        log.info("request-method:{}", request.getMethod());
        log.info("request-url:{}", request.getRequestURL());
        String remoteAddr = request.getRemoteAddr();
        log.info("remote-addr:{}", remoteAddr);
        StringBuffer sb = new StringBuffer();
        request.getParameterMap().forEach((k, v) -> sb.append(k).append(Arrays.toString(v)));
        log.info("request-parameters:{}", sb.toString());
        // 根据ip定位
        //((Runnable) () -> {
        //    try {
        //        String res = HttpUtil.get(TAOBAO_IP_INFO + "?ip=" + remoteAddr);
        //        JSONObject jsonObject = JSONUtil.parseObj(res);
        //        if ("0".equals(jsonObject.get("code"))) {
        //            JSONObject data = jsonObject.getJSONObject("data");
        //            log.info("ip-info:country：{}、area：{}、region：{}、city：{}、isp：{}", data.get("country"), data.get("area"), data.get("region"), data.get("city"), data.get("isp"));
        //        }
        //    } catch (Exception e) {
        //        log.error("ip：{}定位出错", remoteAddr);
        //    }
        //}).run();
        return true;
    }
}
